package com.ipt.frm.pw.listener;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.ipt.frm.factoria.DAOFactory;
import com.ipt.frm.interfaces.InterfaceAlumnoDAO;

/**
 * Application Lifecycle Listener implementation class PropertiesListener
 * 
 */
public class PropertiesListener implements ServletContextListener {

	static Properties propertiesMsg;
	static final String PATH_PROPERTIES_MSG = "/WEB-INF/config/messages.properties";
	ServletContext context;

	private static DAOFactory fMySQL;
	public static InterfaceAlumnoDAO aDao;

	/**
	 * Default constructor.
	 */
	public PropertiesListener() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see ServletContextListener#contextInitialized(ServletContextEvent)
	 */
	@Override
	public void contextInitialized(ServletContextEvent sce) {

		propertiesMsg = new Properties();

		context = sce.getServletContext();

		fMySQL = DAOFactory.getDAOFactory(DAOFactory.MYSQL);
		aDao = fMySQL.getInterfaceAlumnoDAO();

		try {
			propertiesMsg
					.load(context.getResourceAsStream(PATH_PROPERTIES_MSG));
			context.setAttribute("properties", propertiesMsg);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see ServletContextListener#contextDestroyed(ServletContextEvent)
	 */
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		propertiesMsg = null;
	}

}
